<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/friend.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';

class FriendDao {
	private $_db;
	
	public function __construct() {
		$this->_db = new DbConnection();
	}
	
	public function addFriend($friend) {
		$query = sprintf("INSERT INTO friends(UserId, FriendId, Accepted) VALUES ('%d', '%d', %s)", $friend->getUserID(), $friend->getFriendID(), $friend->getAccepted());
		$result = $this->_db->executeQuery($query);
		
		return @mysql_insert_id($result);
	}
	
	public function getFriendsByUserId($userID) {
		$friends = array();
		$query = sprintf("SELECT * FROM friends WHERE UserId = '%d' AND Accepted=TRUE", $userID);
		
		$result = $this->_db->executeQuery($query);
				
		while ($row = mysql_fetch_assoc($result)) {
			$friends[] = $this->setFriendByAssoc($row);
		}
		
		return $friends;
	}
	
	public function getFriendsByFriendId($friendID) {
		$friends = array();
		$query = sprintf("SELECT * FROM friends WHERE FriendId = '%d' AND Accepted=TRUE", $friendID);
				
		$result = $this->_db->executeQuery($query);
				
		while ($row = mysql_fetch_assoc($result)) {			
			$friends[] = $this->setFriendByAssoc($row);
		}
		
		return $friends;
	}
	
	//0: not exist at all
	//1: exist and true
	//2: exist but false
	//3: waiting for accept
	public function isFriend($userID, $friendID) {
		$query = sprintf("SELECT * FROM friends WHERE FriendId = '%d' AND UserId = '%d'", $friendID, $userID);
		
		$result = $this->_db->executeQuery($query);
		
		if (mysql_num_rows($result)) {
			$row = mysql_fetch_assoc($result);
			
			if ($row['Accepted'] == true){
				return 1;
			} else {
				return 2;
			}
		} else {
			$query = sprintf("SELECT * FROM friends WHERE UserId = '%d' AND FriendId = '%d'", $friendID, $userID);
		
			$result = $this->_db->executeQuery($query);
			
			if (mysql_num_rows($result)) {
				return 3;
			}
		}
		
		return 0;
	}
	
	public function isExist($userID, $friendID)	{
		$query = sprintf("SELECT * FROM friends WHERE FriendId = '%d' AND UserId = '%d'", $friendID, $userID);
		
		$result = $this->_db->executeQuery($query);
		
		return mysql_num_rows($result);
	}
	
	public function updateFriendAccept($userID, $friendID) {
		$query = sprintf("UPDATE friends SET Accepted = 1 WHERE UserId= '%d' AND FriendId='%d'", $userID, $friendID);
		
		$result = $this->_db->executeQuery($query);
		
		return $result;
	}
	
	public function getStatus($userID, $friendID) {
		$query = sprintf("SELECT Accepted FROM friends WHERE FriendId = '%d' AND UserId = '%d'", $friendID, $userID);
		$result = $this->_db->executeQuery($query);
		if (!$result) {
   			 die('Could not query:' . mysql_error());
		}
		if (mysql_num_rows($result) > 0) {
			$status = mysql_result($result, 0);
			return $status; // outputs third employee's name
		}
	}
	
	public function getNumberOfFriend($userID) {
		$query = sprintf("SELECT COUNT(*) FROM friends WHERE UserId = '%d' AND Accepted=TRUE", $userID);
		
		$result = $this->_db->executeQuery($query);

		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		if ($row = mysql_fetch_row($result)) {						
			return $row[0];
		}		
	}

	public function getFriendsOfUserFromTo($userID, $from, $to) {
		$query = sprintf("SELECT * FROM friends WHERE UserId = '%d' AND Accepted=TRUE LIMIT %u, %u", $userID, $from, $to);
							
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		$friendList = array();
		while($row = mysql_fetch_assoc($result)) {			
			$friendList[] = $this->setFriendByAssoc($row);
		}
		
		return $friendList;
	} 
	
	public function removeFriend($userID, $friendID) {
		$query = sprintf("CALL sp_RemoveFriend('%d', '%d')", $userID, $friendID);
		
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return 0;
		}
		return 1;
	}
	
	private function setFriendByAssoc($row) {
		$friend = new FriendModel();
			
		$friend->setUserID($row['UserId']);
		$friend->setFriendID($row['FriendId']);
		$friend->setAccepted($row['Accepted']);
				
		return $friend;
	}
}
?>